﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Base.Dao
{
    public class ChangeLogDao : DaoService<ChangeLog>
    {
        public override string TableName => "FAS_ChangeLog";
        public override string IDColumnName => "ID";
        public ChangeLogDao() : base()
        {
            DBName = "TFAS";
        }
        public override List<string> ExceptColumnName => new List<string> { "ChangeDatas" };
        public override Dictionary<string, object> GetDataMapping(ChangeLog data)
        {
            var datas = base.GetDataMapping(data);
            return datas;
        }
        public override ChangeLog ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            var data = base.ConvertToObject(myCol, row);
            return data;
        }
        /// <summary>
        /// 获取变更日志
        /// </summary>
        /// <param name="billId"></param>
        /// <param name="cdoCode"></param>
        /// <returns></returns>
        public List<ChangeLog> GetChangeLogs(string billId, string cdoCode)
        {
            var result = new List<ChangeLog>();
            var objects = GetObjectsByFilter($"BillID = '{billId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as ChangeLog));
            }
            return result;
        }
        /// <summary>
        /// 获取分表变更日志
        /// </summary>
        /// <param name="billId"></param>
        /// <param name="cdoCode"></param>
        /// <param name="tableNameSuf"></param>
        /// <returns></returns>
        public List<ChangeLog> GetBillSplitLogs(string billId, string cdoCode,string tableNameSuf)
        {
            var result = new List<ChangeLog>();
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@billId", TDbDataType.VarChar, billId));
            paramList.Add(DB.MakeInParam("@cdoCode", TDbDataType.VarChar, cdoCode));
            var sql = $@"select ID,UserID,UserName,Createtime,OldStateCode,NewStateCode,OldStateName,NewStateName,CDOName,CDOID,CDOCode,TenantID,BillID,Action 
                    from {TableName}{tableNameSuf} where billId=@billId;";
            var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    var data = ConvertToObject(this.Columns, row);
                    result.Add(data);
                }
            }
            return result;
        }
    }
}
